package com.honey.mybatis.generator;

import com.honey.compilation.java.Clazz;
import com.honey.compilation.java.Decoration;
import com.honey.compilation.java.Field;
import com.honey.compilation.java.JavaVisibility;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.types.JDKFullyQualifiedJavaType;

public abstract class AbstractField extends AbstractMybatis{
	/** 空字符串数组 */
	protected static final String[] EMPTY_STRING_ARRAY =new String[0];
	
	/** 默认的类型 */
	protected static final FullyQualifiedJavaType DEFAULT_FIELD_TYPE= JDKFullyQualifiedJavaType.getStringInstance();
	
	/** 数据库结构 */
	protected IntrospectSchema introspectSchema ;
	
	/** 数据库结构包含的列 */
	protected IntrospectSchemaColumn introspectSchemaColumn;
	
	/** 方法所属的类 */
	protected Clazz clazz;
	/**
	 * 慎用默认的构造函数
	 */
	protected AbstractField(){
		
	}
	
	/**
	 * 构造
	 * @param clazz
	 * @param belongPlugin
	 * @param introspectSchema
	 * @param introspectSchemaColumn
	 */
	protected AbstractField(
			Clazz clazz,
			MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn
			) {
		super(belongPlugin);
		this.clazz = clazz;
		this.introspectSchema = introspectSchema;
		this.introspectSchemaColumn = introspectSchemaColumn;
	}
	
	public final Field createField(){
		Field answer = null;
		String name =  getFieldName();
		if( name != null){
			answer = new Field(name,getType(),getDecoration());
			answer.addJavaDocLine(getJavaDoc());
			answer.addAnnotation(getAnnotation());
			answer.setBaseType(isBaseType());
			answer.setInitializationString(getInitialization(), getIsformat());
			answer.setUseDefaultValue(isUseDefaultValue());
			
		}
		return answer ;
	}
	
	protected abstract String getFieldName();
	
	/**
	 * 属性类型,默认为Stirng类型.
	 * @return
	 */
	protected FullyQualifiedJavaType getType(){
		
		return DEFAULT_FIELD_TYPE;
	};
	
	/**
	 * 属性修饰,默认使用private修饰.
	 * @return
	 */
	protected Decoration getDecoration(){
		return new Decoration(JavaVisibility.PRIVATE);
	}
	
	/**
	 * 属性初始值
	 * @return
	 */
	protected String getInitialization(){
		return null;
	}
	
	/**
	 * 是否格式化初始值,默认是true.
	 * @return
	 */
	protected boolean getIsformat(){
		return true;
	}
	
	/**
	 * 是否使用基础类型,默认是true.
	 * @return
	 */
	protected boolean isBaseType(){
		return true;
	}
	
	/**
	 * 是否使用类型默认的值作为初始值,默认是true.
	 * @return
	 */
	protected boolean isUseDefaultValue(){
		return true;
	}
	
	/**
	 * 注释
	 * @return
	 */
	protected String[] getJavaDoc(){
		return EMPTY_STRING_ARRAY;
	}
	
	/**
	 * 方法注解,默认为空.
	 * @return
	 */
	protected String[] getAnnotation(){
		return EMPTY_STRING_ARRAY;
	}
}